﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Windows.Forms;

namespace IDCardInput
{
    public partial class FmMain : Form
    {
        //SpeechRecognizer rec = new SpeechRecognizer();
        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN"));
        public FmMain()
        {
            InitializeComponent();
            //为语音识别事件添加处理程序。 
            //rec.SpeechRecognized += rec_SpeechRecognized; 
        }
        void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            txtIDCardNo.Text = e.Result.Text;
        }
        private void FmMain_Load(object sender, EventArgs e)
        {
            //初始化命令词 
            var c = new Choices();
            //添加命令词 
            for (var i = 0; i <= 9; i++)
                c.Add(i.ToString());
            c.Add("X");
            //将命令词添加到管理中 
            var gb = new GrammarBuilder(c);
            //实例化命令词管理 
            var g = new Grammar(gb);
            //创建并加载听写语法(添加命令词汇识别的比较精准) 
            //rec.LoadGrammar(g);
            //rec.Enabled = true;
            recognizer.LoadGrammar(g);
            //为语音识别事件添加处理程序。 
            recognizer.SpeechRecognized += rec_SpeechRecognized;
            //将输入配置到语音识别器。  
            recognizer.SetInputToDefaultAudioDevice();
            //启动连续语音识别。  
            recognizer.Recognize();

        }
    }
}
